
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;

namespace BlazeApps.Library.News
{
    [Serializable()]
    public class NewsViewChild : Csla.ReadOnlyBase<NewsViewChild>
    {
        #region Business Properties and Methods

        //declare members
        private Guid _id = Guid.NewGuid();
        private string _title = string.Empty;
        private string _body = string.Empty;
        private SmartDate _createdate = new SmartDate(DateTime.Now);
        private SmartDate _expiredate = new SmartDate(false);
        private bool _archived = false;
        private int _comments = 0;

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid id
        {
            get
            {
                CanReadProperty("id", true);
                return _id;
            }
        }

        public string title
        {
            get
            {
                CanReadProperty("title", true);
                return _title;
            }
        }

        public string body
        {
            get
            {
                CanReadProperty("body", true);
                return _body;
            }
        }

        public DateTime createdate
        {
            get
            {
                CanReadProperty("createdate", true);
                return _createdate.Date;
            }
        }

        public string createdateString
        {
            get
            {
                CanReadProperty("createdate", true);
                return _createdate.Text;
            }
        }

        public DateTime expiredate
        {
            get
            {
                CanReadProperty("expiredate", true);
                return _expiredate.Date;
            }
        }

        public string expiredateString
        {
            get
            {
                CanReadProperty("expiredate", true);
                return _expiredate.Text;
            }
        }

        public bool archived
        {
            get
            {
                CanReadProperty("archived", true);
                return _archived;
            }
        }

        public int Comments
        {
            get
            {
                CanReadProperty("Comments", true);
                return _comments;
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion //Business Properties and Methods

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in NewsViewChild
            //AuthorizationRules.AllowRead("id", "NewsViewChildReadGroup");
            //AuthorizationRules.AllowRead("title", "NewsViewChildReadGroup");
            //AuthorizationRules.AllowRead("body", "NewsViewChildReadGroup");
            //AuthorizationRules.AllowRead("createdate", "NewsViewChildReadGroup");
            //AuthorizationRules.AllowRead("expiredate", "NewsViewChildReadGroup");
            //AuthorizationRules.AllowRead("archived", "NewsViewChildReadGroup");

        }

        #endregion //Authorization Rules

        #region Factory Methods
        internal static NewsViewChild GetNewsViewChild(SafeDataReader dr)
        {
            return new NewsViewChild(dr);
        }

        private NewsViewChild(SafeDataReader dr)
        {
            Fetch(dr);
        }
        #endregion //Factory Methods

        #region Data Access

        #region Data Access - Fetch
        private void Fetch(SafeDataReader dr)
        {
            FetchObject(dr);

            //load child object(s)
            FetchChildren(dr);
        }

        private void FetchObject(SafeDataReader dr)
        {
            _id = dr.GetGuid("newsid");
            _title = dr.GetString("title");
            _body = dr.GetString("body");
            //_createdate = dr.GetSmartDate("createdate", _createdate.EmptyIsMin);
            _expiredate = dr.GetSmartDate("expiredate", _expiredate.EmptyIsMin);
            //_archived = dr.GetBoolean("archived");
            _comments = dr.GetInt32("comments");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
